package me.zhengjie.modules.transformer.service.impl;

import me.zhengjie.modules.transformer.domain.FdsData;
import me.zhengjie.modules.transformer.service.FileService;
import me.zhengjie.modules.transformer.service.FdsDataService;
import me.zhengjie.modules.utils.CSVUtils;
import me.zhengjie.service.LocalStorageService;
import me.zhengjie.utils.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.List;

/**
 * FileServiceImpl
 *
 * @author weiruitao
 * @date 2021/12/1
 */
@Service
public class FileServiceImpl implements FileService {

    @Autowired
    CSVUtils csvUtils;

    @Autowired
    FdsDataService fdsDataService;

    @Autowired
    LocalStorageService localStorageService;

    @Override
    public int handleFdsData(File file) {
        List<List<String>> data = csvUtils.getData(file, "[FDSData]");
        List<FdsData> fdsData = csvUtils.packingFdsData(data);
        return fdsDataService.create(fdsData) == null ? 0 : 1;
    }

    @Override
    public int receiveFile(MultipartFile multipartFile, String field) {
        File file = FileUtil.toFile(multipartFile);
        List<List<String>> data = csvUtils.getData(file, field);
        System.out.println(data);
        List<FdsData> fdsData = csvUtils.packingFdsData(data);
        return fdsDataService.create(fdsData) == null ? 0 : 1;
    }
}
